package com.coocaa.internship.controller;

import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.pojo.Company;
import com.coocaa.internship.service.serviceImpl.CompanyServiceImpl;
import com.coocaa.internship.util.BindingResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @Description:
 * @Author: zwh
 * @Date: 2019-07-29
 * @Time: 22:26
 */
@Slf4j
@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    CompanyServiceImpl companyService;


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public ResultData insertCompany(@RequestParam(value = "logo") MultipartFile logo, @Valid Company company, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            BindingResultUtil.resolveErrors(bindingResult);
        }
        return companyService.insertCompany(logo, company);
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    @ResponseBody
    public ResultData deleteCompany(@RequestParam("companyId") Long companyId) {
        return companyService.deleteCompanyById(companyId);
    }

    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ResponseBody
    public ResultData selectCompanyById(@RequestParam("companyId") Long companyId) {
        return companyService.selectCompanyById(companyId);
    }

    @RequestMapping(value = "/selectByName", method = RequestMethod.GET)
    @ResponseBody
    public ResultData selectCompanyByName(@RequestParam("companyName") String companyName) {
        return companyService.selectCompaniesByName(companyName);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultData updateCompanyById(@RequestParam(value = "logo", required = false) MultipartFile logo, Company company) {
        return companyService.updateCompanyById(logo, company);
    }
}
